﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using B4Deal.Models;

namespace B4Deal.Controllers
{
    public class UserInfoController : Controller
    {
        MyDataMembershipProvider membership = new MyDataMembershipProvider();
        UserManager userManager = new UserManager();
        //
        // GET: /UserInfo/
        string[] theme1 = new string[] { "~/Content/UserThemes/profile.css", "~/Content/UserThemes/mainMP.css", "~/Content/UserThemes/main.css" };
        string[] theme2 = new string[] { "~/Content/UserThemes/profile.css", "~/Content/UserThemes/mainMP.css", "~/Content/UserThemes/main.css" };

        public ActionResult Index()
        {
            if (Session["logon"] == null || !Session["logon"].Equals("on"))
                return RedirectToAction("Index", "Home");
            ViewBag.partial = "_ttcn";
            ViewBag.active = 1;
            TempData["theme"] = theme1;
            ViewBag.cssLink = TempData.Peek("theme");
            TAIKHOAN user = membership.GetUser(Session["user"].ToString());
            return View(user);
        }

        public ActionResult DislayView(string name)
        {
            if (Session["logon"] == null || !Session["logon"].Equals("on"))
                return RedirectToAction("Index", "Home");
            string theme = Request.Form["choosethemes"];
            if (theme != null)
            {
                TempData["theme"] = (theme == "theme1") ? theme1 : theme2;
            }
            ViewBag.cssLink = TempData.Peek("theme");
            switch (name)
            {
                case "ttdh":
                    {
                        ViewBag.partial = "_ttdh";
                        ViewBag.active = 2;
                    } break;
                case "ttdn":
                    {
                        ViewBag.partial = "_ttdn";
                        ViewBag.active = 3;
                    } break;
                case "hdd":
                    {
                        ViewBag.partial = "_hdd";
                        ViewBag.active = 1;
                    } break;
                case "ttcn":
                    {
                        ViewBag.partial = "_ttcn";
                        ViewBag.active = 1;
                    } break;
                case "tthv":
                    {
                        ViewBag.partial = "_tthv";
                        ViewBag.active = 1;
                    } break;
                case "ttst":
                    {
                        ViewBag.partial = "_ttst";
                        ViewBag.active = 1;
                    } break;
                case "ttlh":
                    {
                        ViewBag.partial = "_ttlh";
                        ViewBag.active = 1;
                    } break;
                default:
                    {
                        ViewBag.partial = "_ttcn";
                        ViewBag.active = 1;
                    } break;
            }
            MultiModel model;
            TAIKHOAN user = membership.GetUser(Session["user"].ToString());
            if (ViewBag.active == 3)
            {
                bool has = userManager.checkBusiness(user);
                DOANHNGHIEP bus = null;
                if (has)
                    bus = userManager.getBusiness(user);
                if (bus == null)
                    bus = new DOANHNGHIEP();
                model = new MultiModel(user, bus);
            }
            else
            {
                model = new MultiModel(user);
            }
            return View("DislayView", model);
        }

        public PartialViewResult ViewOrders()
        {
            TAIKHOAN user = membership.GetUser(Session["user"].ToString());
            List<Order> lst = userManager.GetOrdersByUser(user);
            return PartialView("_RowHOrder", lst);
        }

        [HttpPost]
        public ActionResult ModifyInfo()
        {
            if (Request.IsAjaxRequest())
            {
                String oPass = Request.Form["oldUserPass"].ToString();
                String nPass = Request.Form["newUserPass"].ToString();
                String vnPass = Request.Form["newVerifyPass"].ToString();
                int d = int.Parse(Request.Form["userBornDay"]);
                int m = int.Parse(Request.Form["userBornMonth"]);
                int y = int.Parse(Request.Form["userBornYear"]);
                String gioitinh = Request.Form["userGT"].ToString();
                try
                {
                    bool gt = (gioitinh.CompareTo("GTNam") == 0) ? true : false;
                    membership.modifyUser(Session["user"].ToString(), d, m, y, gt);
                }
                catch (Exception ex)
                {
                    return Json(new { rs = "fail", detail = ex.Message.ToString() });
                }
                // String changePass = null;
                if (Request.Form.Count > 9)
                {
                    //changePass = Request.Form["chanPassword"].ToString();
                    //  if (changePass != null && changePass.Equals("on"))
                    // {
                    if (oPass.Length < 6 || nPass.Length < 6 || vnPass.Length < 6)
                    {
                        return Json(new { rs = "fail", detail = "Mật khẩu phải từ 6 - 12 ký tự!" });
                    }
                    else
                    {
                        if (nPass.CompareTo(vnPass) == 0)
                        {
                            try
                            {
                                bool sc = membership.ChangePassword(Session["user"].ToString(),
                                    oPass, nPass);
                                if (!sc)
                                    return Json(new { rs = "fail", detail = "Bạn nhập sai password!" });
                            }
                            catch (Exception ex)
                            {
                                return Json(new { rs = "fail", detail = ex.Message });
                            }
                            return Json(new { rs = "success", detail = "Lưu thông tin thành công!" });
                        }
                        else
                            return Json(new { rs = "fail", detail = "Xác nhận password mới không khớp!" });
                    }
                }
                else if (oPass.Length > 0 || nPass.Length > 0 || vnPass.Length > 0)
                {
                    return Json(new { rs = "fail", detail = "Xin vui lòng check thay đổi mật khẩu!" });
                }
            }
            return RedirectToAction("DislayView", new { name = "ttcn" });
        }

        [HttpPost]
        public ActionResult ModifyBusiness()
        {
            if (Request.IsAjaxRequest())
            {                
                String nameBUS = Request.Form["busName"].ToString();
                String levelBUS = Request.Form["busLevel"].ToString();
                String webBUS = Request.Form["busWeb"].ToString();
                String addrBUS = Request.Form["busAddr"].ToString();
                String typeBUS = Request.Form["typeBusInfo"].ToString();
                String infoBUS = Request.Form["txtConcept"].ToString();

                //Validate
                if (Request.Form.Count < 7) //Don't accept
                    return Json(new { rs = "fail", detail = "Bạn chưa thỏa thuận điều khoản kinh doanh!" });
                if (String.IsNullOrEmpty(nameBUS) || String.IsNullOrEmpty(levelBUS) ||
                    String.IsNullOrEmpty(webBUS) || String.IsNullOrEmpty(addrBUS) ||
                    String.IsNullOrEmpty(typeBUS))
                    return Json(new { rs = "fail", detail = "Bạn phải điền đầy đủ các thông tin bắt buộc *!" });

                //Modify
                bool sc = false;
                try
                {
                    sc = userManager.modifyBusiness(Session["user"].ToString(), nameBUS, levelBUS,
                                            webBUS, addrBUS, typeBUS, infoBUS);
                }
                catch (Exception ex)
                {
                    return Json(new { rs = "fail", detail = ex.Message });
                }
                if (!sc)
                    return Json(new { rs = "fail", detail = "Có lỗi xảy ra khi cập nhật! Vui lòng thử lại!" });
                return Json(new { rs = "success", detail = "Lưu thông tin thành công!" });
            }
            return RedirectToAction("DislayView", new { name = "ttcn" });
        }              
    }
}
